package com.fishingwithme.android

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.rememberNavController
import com.fishingwithme.android.data.local.PreferencesManager
import com.fishingwithme.android.navigation.FishingNavHost
import com.fishingwithme.android.navigation.FishingNavigation
import com.fishingwithme.android.ui.components.BottomNavigationBar
import com.fishingwithme.android.ui.components.LocationPermissionDialog
import com.fishingwithme.android.ui.theme.FishingWithMeTheme
import com.fishingwithme.android.ui.viewmodels.AuthViewModel
import com.fishingwithme.android.ui.viewmodels.StartupViewModel
import com.fishingwithme.android.utils.EventBus
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    
    @Inject
    lateinit var preferencesManager: PreferencesManager
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        setContent {
            val themeMode by preferencesManager.getThemeMode().collectAsState(
                initial = PreferencesManager.THEME_FOLLOW_SYSTEM
            )
            
            FishingWithMeTheme(themeMode = themeMode) {
                val authViewModel: AuthViewModel = hiltViewModel()
                val authUiState by authViewModel.uiState.collectAsState()
                val navController = rememberNavController()
                
                // Check login status and navigate accordingly
                LaunchedEffect(authUiState.isLoggedIn) {
                    if (!authUiState.isLoggedIn) {
                        navController.navigate(FishingNavigation.LOGIN) {
                            // Clear the back stack so user can't go back to main app without logging in
                            popUpTo(0) { inclusive = true }
                        }
                    }
                }
                
                // If logged in, show main app content
                if (authUiState.isLoggedIn) {
                    val startupViewModel: StartupViewModel = hiltViewModel()
                    val startupUiState by startupViewModel.uiState.collectAsState()
                    
                    // Observe unauthorized events from ViewModels
                    LaunchedEffect(Unit) {
                        launch {
                            EventBus.unauthorizedEvent.collect {
                                // Navigate to login screen when unauthorized
                                navController.navigate(FishingNavigation.LOGIN) {
                                    // Clear the back stack so user can't go back to main app without logging in
                                    popUpTo(0) { inclusive = true }
                                }
                            }
                        }
                    }
                    
                    Surface(
                        modifier = Modifier.fillMaxSize(),
                        color = MaterialTheme.colorScheme.background
                    ) {
                        // Main app structure with bottom navigation
                        // Similar to WeChat Mini Program's tabBar structure
                        BottomNavigationBar(navController = navController) {
                            FishingNavHost(navController = navController)
                        }
                        
                        // Location permission dialog on startup
                        LocationPermissionDialog(
                            showDialog = startupUiState.showLocationPermissionDialog,
                            onPermissionResult = { isGranted ->
                                startupViewModel.onLocationPermissionResult(isGranted)
                            },
                            onDismiss = {
                                startupViewModel.dismissLocationPermissionDialog()
                            }
                        )
                    }
                } else {
                    FishingNavHost(
                        navController = navController,
                        startDestination = FishingNavigation.LOGIN
                    )
                }
            }
        }
    }
}